package com.spark.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spark.common.annotation.DictTranslate;
import com.spark.common.annotation.Log;
import com.spark.common.constants.validGroup.AddGroup;
import com.spark.common.constants.validGroup.EditGroup;
import com.spark.common.core.Result;
import com.spark.common.core.entity.system.SysUser;
import com.spark.common.enums.BusinessType;
import com.spark.common.query.QueryGenerator;
import com.spark.modules.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 系统用户Controller
 *
 * @author LYCHEE
 * @date 2025/02/18 09:12
 */
@RestController
@RequestMapping(value = "/sys/user")
public class SysUserController {

    @Resource
    private ISysUserService sysUserService;

    /**
     * 分页列表查询
     *
     * @param sysUser  系统用户对象
     * @param pageNo   分页
     * @param pageSize 分页
     * @return 系统用户列表
     */
    @DictTranslate
    @PreAuthorize("@sp.hasPerm('sys:user:query')")
    @Log(moduleName = "系统用户", businessType = BusinessType.SELECT)
    @GetMapping(value = "/queryPageList")
    public Result<?> queryPageList(SysUser sysUser,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        QueryWrapper<SysUser> queryWrapper = QueryGenerator.initQueryWrapper(sysUser);
        Page<SysUser> page = new Page<SysUser>(pageNo, pageSize);
        IPage<SysUser> pageList = sysUserService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 列表查询
     *
     * @param sysUser 系统用户对象
     * @return 系统用户列表
     */
    @DictTranslate
    @PreAuthorize("@sp.hasPerm('sys:user:query')")
    @Log(moduleName = "系统用户", businessType = BusinessType.SELECT)
    @GetMapping(value = "/queryList")
    public Result<?> queryList(SysUser sysUser) {
        QueryWrapper<SysUser> queryWrapper = QueryGenerator.initQueryWrapper(sysUser);
        List<SysUser> list = sysUserService.list(queryWrapper);
        return Result.ok(list);
    }

    /**
     * 通过ID查询
     *
     * @param id 主键
     * @return 系统用户对象
     */
    @DictTranslate
    @PreAuthorize("@sp.hasPerm('sys:user:queryById')")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam String id) {
        return sysUserService.queryById(id);
    }

    /**
     * 新增
     *
     * @param sysUser 系统用户对象
     * @return 成功或失败
     */
    @PreAuthorize("@sp.hasPerm('sys:user:add')")
    @PostMapping(value = "/add")
    public Result<?> add(@Validated(AddGroup.class) @RequestBody SysUser sysUser) {
        return sysUserService.add(sysUser);
    }

    /**
     * 编辑
     *
     * @param sysUser 系统用户对象
     * @return 成功或失败
     */
    @PreAuthorize("@sp.hasPerm('sys:user:edit')")
    @PostMapping(value = "/edit")
    public Result<?> edit(@Validated(EditGroup.class) @RequestBody SysUser sysUser) {
        return sysUserService.edit(sysUser);
    }

    /**
     * 通过id删除
     *
     * @param id 主键
     * @return 成功或失败
     */
    @PreAuthorize("@sp.hasPerm('sys:user:del')")
    @DeleteMapping(value = "/delete/{id}")
    public Result<?> delete(@PathVariable(name = "id") String id) {
        boolean update = sysUserService.removeById(id);
        return Result.flag(update);
    }

    /**
     * 通过id批量删除删除
     *
     * @param ids 主键
     * @return 成功或失败
     */
    @PreAuthorize("@sp.hasPerm('sys:user:batchDel')")
    @DeleteMapping(value = "/deleteBatchByIds/{ids}")
    public Result<?> deleteBatchByIds(@PathVariable(name = "ids") String[] ids) {
        boolean update = sysUserService.removeBatchByIds(Arrays.asList(ids));
        return Result.flag(update);
    }

    /**
     * 重置密码为：spark@123
     *
     * @return 成功或失败
     */
    @PostMapping(value = "/resetPassWord")
    public Result<?> resetPassWord() {
        return sysUserService.resetPassWord();
    }
}
